<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>汽车数据展示</title>
  <style>
    table,
    th,
    td {
      border: 1px solid #ccc;
      border-collapse: collapse;
      width: 800px;
      padding: 15px;
      text-align: center;
    }
  </style>
</head>

<body>
  <input type="text" name="brand" placeholder="请您输入汽车品牌" />
  <button type="button">按照品牌搜索</button>
  <br /><br />
  <input type="text" name="start" /> ~
  <input type="text" name="end" />
  <button type="button">按照价格区间搜索</button>
  <hr />
  <table>
    <thead>
      <th>序号</th>
      <th>品牌</th>
      <th>型号</th>
      <th>价格</th>
    </thead>
    <tbody id="data">
    </tbody>
  </table>

  <script src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/jquery/3.6.0/jquery.min.js" type="application/javascript"></script>
  <script>
    //jquery页面加载完毕事件
    $(function () {
      //初始化页面数据
      $.get('http://192.168.110.2:8888/AjaxTeach/car/all', function (cars) {
        cars = JSON.parse(cars)
        cars.forEach(car => {
          $('#data').append(`<tr><td>${car.id}</td><td>${car.brand}</td><td>${car.type}</td><td>${car.price}</td></tr>`)
        })
      })

      //按照品牌搜索
      $('button').first().click(function () {
        //清空表格原始数据
        $("#data").empty()
        $.get('http://192.168.110.2:8888/AjaxTeach/car/brand?brand=' + $(this).prev().val(), function (cars) {
          cars = JSON.parse(cars)
          cars.forEach(car => {
            $('#data').append(`<tr><td>${car.id}</td><td>${car.brand}</td><td>${car.type}</td><td>${car.price}</td></tr>`)
          })
        })
      })

      //按照价格区间搜索
      $('button').last().click(function () {
        let startPrice = $(this).prev().prev().val()
        let endPrice = $(this).prev().val()
        if (startPrice == '') {
          startPrice = 0
        }
        if (endPrice == '') {
          endPrice = 10000000
        }
        if (startPrice < endPrice) {
          $("#data").empty()
          $.get(`http://192.168.110.2:8888/AjaxTeach/car/price?start=${startPrice}&end=${endPrice}`, function (cars) {
            cars = JSON.parse(cars)
            cars.forEach(car => {
              $('#data').append(`<tr><td>${car.id}</td><td>${car.brand}</td><td>${car.type}</td><td>${car.price}</td></tr>`)
            })
          })
        }

      })
    })
  </script>
</body>

</html>